/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConsultaAnunciosView.java
 *
 * Created on 27-ene-2011, 1:31:18
 */

package Vista.Anuncio;

import Modelo.Entidades.Anuncio;
import Vista.SesionActual;
import java.util.List;

/*
 *  Proyecto eJAM [G12]
 *  Miembros:
 *      - Javier Ruiz Hidalgo
 *      - Adrian Ramos Cápitas
 *      - Miguel Alejandro Esteban Ordóñez
 */

public class ConsultaAnunciosView extends javax.swing.JPanel {

    private AnuncioViewImpl view;
    private AnuncioTableModel anuncioTableModel;

    /** Creates new form ConsultaAnunciosView */
    public ConsultaAnunciosView(AnuncioViewImpl view) {
        this.view = view;
        this.anuncioTableModel = AnuncioTableModel.create();

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelTablaAnuncios = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTablaAnuncios = new javax.swing.JTable();
        jAbrir = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        jPanelTablaAnuncios.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Bandeja Entrada Anuncios", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jTablaAnuncios.setModel(anuncioTableModel);
        jScrollPane1.setViewportView(jTablaAnuncios);

        javax.swing.GroupLayout jPanelTablaAnunciosLayout = new javax.swing.GroupLayout(jPanelTablaAnuncios);
        jPanelTablaAnuncios.setLayout(jPanelTablaAnunciosLayout);
        jPanelTablaAnunciosLayout.setHorizontalGroup(
            jPanelTablaAnunciosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTablaAnunciosLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 648, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelTablaAnunciosLayout.setVerticalGroup(
            jPanelTablaAnunciosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTablaAnunciosLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
                .addContainerGap())
        );

        jAbrir.setText("Abrir");
        jAbrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAbrirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelTablaAnuncios, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 680, Short.MAX_VALUE)
                    .addComponent(jAbrir, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelTablaAnuncios, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jAbrir)
                .addContainerGap(222, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jAbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAbrirActionPerformed
        AnuncioTableModel ttm = (AnuncioTableModel)this.jTablaAnuncios.getModel();
        int row = this.jTablaAnuncios.getSelectedRow();

        if(row != -1){
            Anuncio anuncio = ttm.getRow(row);
            new VentanaEmergenteView(anuncio.getDescripcion());
            this.view.getController().marcaComoLeidoGesture(anuncio.getID());
        }
    }//GEN-LAST:event_jAbrirActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAbrir;
    private javax.swing.JPanel jPanelTablaAnuncios;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTablaAnuncios;
    // End of variables declaration//GEN-END:variables

    public void refrescaVista() {
        String DNI = SesionActual.instancia().obtieneSesion().getUsuario().getDNI();
        List<Anuncio> anuncios = this.view.getModel().obtieneAnunciosByDNI(DNI);

        this.anuncioTableModel.setAnuncios(anuncios);
    }

}
